package com.jinzecloud.reportapproval.service;

import com.jinzecloud.reportapproval.bean.RoleEntity;
import com.jinzecloud.reportapproval.form.RoleAddForm;
import com.jinzecloud.reportapproval.form.RoleUpdateForm;
import com.jinzecloud.reportapproval.mapper.RoleMapper;
import com.jinzecloud.reportapproval.mapper.RoleMenuMapper;
import com.jinzecloud.reportapproval.mapper.RoleUserMapper;
import com.jinzecloud.reportapproval.utils.R;
import com.jinzecloud.reportapproval.utils.SmartBeanUtil;
import com.jinzecloud.reportapproval.vo.RoleVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色
 *
 * @Author 1024创新实验室: 胡克
 * @Date 2021-08-16 20:19:22
 * @Wechat zhuoda1024
 * @Email lab1024@163.com
 * @Copyright <a href="https://1024lab.net">1024创新实验室</a>
 */
@Service
public class RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuDao;

    @Resource
    private RoleUserMapper roleUserMapper;

    /**
     * 新增添加角色
     */
    public R addRole(RoleAddForm roleAddForm) {
        RoleEntity existRoleEntity = roleMapper.getByRoleName(roleAddForm.getRoleName());
        if (null != existRoleEntity) {
            return R.error("角色名称重复");
        }

        existRoleEntity = roleMapper.getByRoleCode(roleAddForm.getRoleCode());
        if (null != existRoleEntity) {
            return R.error("角色编码重复，重复的角色为：" + existRoleEntity.getRoleName());
        }

        RoleEntity roleEntity = SmartBeanUtil.copy(roleAddForm, RoleEntity.class);
        roleMapper.insert(roleEntity);
        return R.ok();
    }

    /**
     * 根据角色id 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public R deleteRole(Long roleId) {
        RoleEntity roleEntity = roleMapper.selectById(roleId);
        if (null == roleEntity) {
            return R.error();
        }
        roleMapper.deleteById(roleId);
        roleMenuDao.deleteByRoleId(roleId);
        roleUserMapper.deleteByRoleId(roleId);
        return R.ok();
    }

    /**
     * 更新角色
     */
    @Transactional(rollbackFor = Exception.class)
    public R updateRole(RoleUpdateForm roleUpdateForm) {
        if (null == roleMapper.selectById(roleUpdateForm.getRoleId())) {
            return R.error();
        }

        RoleEntity existRoleEntity = roleMapper.getByRoleName(roleUpdateForm.getRoleName());
        if (null != existRoleEntity && !existRoleEntity.getRoleId().equals(roleUpdateForm.getRoleId())) {
            return R.error("角色名称重复");
        }

        existRoleEntity = roleMapper.getByRoleCode(roleUpdateForm.getRoleCode());
        if (null != existRoleEntity) {
            return R.error("角色编码重复，重复的角色为：" + existRoleEntity.getRoleName());
        }

        RoleEntity roleEntity = SmartBeanUtil.copy(roleUpdateForm, RoleEntity.class);
        roleMapper.updateById(roleEntity);
        return R.ok();
    }

    /**
     * 根据id获取角色数据
     */
    public R getRoleById(Long roleId) {
        RoleEntity roleEntity = roleMapper.selectById(roleId);
        if (null == roleEntity) {
            return R.error();
        }
        RoleVO role = SmartBeanUtil.copy(roleEntity, RoleVO.class);
        return R.ok().put("data",role);
    }

    /**
     * 获取所有角色列表
     */
    public R getAllRole() {
        List<RoleEntity> roleEntityList = roleMapper.selectList(null);
        List<RoleVO> roleList = SmartBeanUtil.copyList(roleEntityList, RoleVO.class);
        return R.ok().put("data",roleList);
    }
}
